package com.xieyong.mywebsite.interceptors;

import com.xieyong.mywebsite.exception.MyCustomizeException;
import com.xieyong.mywebsite.util.ThreadLocalUtil;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

import java.util.Map;

@Component
public class AdminInterceptor implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler){
        Map<String, Object> map = ThreadLocalUtil.get();
        if (map.get("username").equals("admin")) {
            return true;
        } else {
            throw new MyCustomizeException("您无权限操作");
        }
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex){
        ThreadLocalUtil.remove();
    }
}
